package com.wolfalpha.utron.service.impl;

import android.text.TextUtils;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemUActivity;
import com.wolfalpha.utron.bean.UActivity;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.ActivityService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luodon on 15/9/5.
 */
public class ActivityServiceImpl implements ActivityService {
    @Override
    public List<ItemUActivity> getList(int page) {

        List<ItemUActivity> activities = new ArrayList<ItemUActivity>();
        String result = AppContext.getApiClient().sendGetResquest(URLs.ACTIVITY_URL + "getList&page=" + String.valueOf(page));
        if (result == null) {
            return null;
        }
        System.out.println(result);
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemUActivity activity = JsonUtils.paraseJson(arr.get(i).toString(), ItemUActivity.class);
                activities.add(activity);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return activities;
    }

    @Override
    public UActivity getDetail(int uid) {
        String result = AppContext.getApiClient().sendGetResquest(URLs.ACTIVITY_URL + "getDetail&activity_id=" + String.valueOf(uid));
        if (result == null && !TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), UActivity.class);
    }
}
